import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

const state = {
  userId: "",
  token: "",
  username: "",
  email: "",
  auth: null,
  photo: "",
  ws: null
};



const mutations = {
  login(state, data) {
    state.userId = data.id;
    state.token = data.token;
    state.username = data.name;
    state.email = data.email;
    state.auth = data.auth;
    state.photo = `${process.env.VUE_APP_SERVER_HOST}${data.photo}`
    state.ws = new WebSocket(`${process.env.VUE_APP_WEBSOCKET_HOST}/${data.id}`)
    localStorage.setItem("store", JSON.stringify(state));
  },
  outlogin(state, that) {
    state.userId = "";
    state.token = "";
    state.username = "";
    state.email = "";
    state.auth = "";
    state.email = "";
    state.ws=null;
    window.localStorage.clear();
    that.$router.push("/login");
  },
  setPhoto(state, photo) {
    state.photo = `${process.env.VUE_APP_SERVER_HOST}${photo}`
    localStorage.setItem("store", JSON.stringify(state));
  },
  setWs(state) {
    state.ws = new WebSocket(`${process.env.VUE_APP_WEBSOCKET_HOST}/${state.userId}`)
  },
  setuser(state,user){
    if(user.name){
       state.username=user.name
    }
    if(user.email){
       state.email=user.email
    }
  }
};




export default new Vuex.Store({
  state,
  mutations
});
